package pl.edu.agh.mgr.hotel.ws.configuration;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import pl.edu.agh.mgr.hotel.ws.client.City;
import pl.edu.agh.mgr.hotel.ws.client.ServiceDescription;
import pl.edu.agh.mgr.hotel.ws.client.WSClient;
import pl.edu.agh.mgr.hotel.ws.client.WSClient1;
import pl.edu.agh.mgr.hotel.ws.client.WSClient10;
import pl.edu.agh.mgr.hotel.ws.client.WSClient11;
import pl.edu.agh.mgr.hotel.ws.client.WSClient12;
import pl.edu.agh.mgr.hotel.ws.client.WSClient13;
import pl.edu.agh.mgr.hotel.ws.client.WSClient14;
import pl.edu.agh.mgr.hotel.ws.client.WSClient15;
import pl.edu.agh.mgr.hotel.ws.client.WSClient16;
import pl.edu.agh.mgr.hotel.ws.client.WSClient17;
import pl.edu.agh.mgr.hotel.ws.client.WSClient18;
import pl.edu.agh.mgr.hotel.ws.client.WSClient19;
import pl.edu.agh.mgr.hotel.ws.client.WSClient2;
import pl.edu.agh.mgr.hotel.ws.client.WSClient20;
import pl.edu.agh.mgr.hotel.ws.client.WSClient21;
import pl.edu.agh.mgr.hotel.ws.client.WSClient22;
import pl.edu.agh.mgr.hotel.ws.client.WSClient23;
import pl.edu.agh.mgr.hotel.ws.client.WSClient24;
import pl.edu.agh.mgr.hotel.ws.client.WSClient25;
import pl.edu.agh.mgr.hotel.ws.client.WSClient26;
import pl.edu.agh.mgr.hotel.ws.client.WSClient27;
import pl.edu.agh.mgr.hotel.ws.client.WSClient28;
import pl.edu.agh.mgr.hotel.ws.client.WSClient29;
import pl.edu.agh.mgr.hotel.ws.client.WSClient3;
import pl.edu.agh.mgr.hotel.ws.client.WSClient30;
import pl.edu.agh.mgr.hotel.ws.client.WSClient4;
import pl.edu.agh.mgr.hotel.ws.client.WSClient5;
import pl.edu.agh.mgr.hotel.ws.client.WSClient6;
import pl.edu.agh.mgr.hotel.ws.client.WSClient7;
import pl.edu.agh.mgr.hotel.ws.client.WSClient8;
import pl.edu.agh.mgr.hotel.ws.client.WSClient9;
import pl.edu.agh.mgr.hotel.ws.client.utility.IWSClient;

public class WSUtil {

	private static Map<String, PeersCache> clientPeers = new HashMap<String, PeersCache>();

	private static Map<String, String> wsNames = new HashMap<String, String>();

	private static List<IWSClient> clients = new ArrayList<>();

	// private static Logger log = Logger.getLogger("WSUtil");

	private static int dataLevel = -1;

	private static Date lastModifiedDate = Calendar.getInstance().getTime();
	
	private static final City C0 = new City("C0", dataLevel);
	private static final City C1 = new City("C1", dataLevel);
	private static final City C2 = new City("C2", dataLevel);
	private static final City C3 = new City("C3", dataLevel);
	private static final City C4 = new City("C4", dataLevel);
	private static final City C5 = new City("C5", dataLevel);

	static {
		clientPeers.put("http://localhost:8081/HotelWS/services/DataServicePort",new PeersCache(new ServiceDescription("H1","http://localhost:8081/HotelWS/services/DataService1Port", dataLevel, lastModifiedDate, C0)));
		clientPeers.put("http://localhost:8081/HotelWS/services/DataService1Port",new PeersCache(new ServiceDescription("H2","http://localhost:8081/HotelWS/services/DataService2Port", dataLevel, lastModifiedDate, C1)));
		clientPeers.put("http://localhost:8081/HotelWS/services/DataService2Port",new PeersCache(new ServiceDescription("H3","http://localhost:8081/HotelWS/services/DataService3Port", dataLevel, lastModifiedDate, C2)));
		
//		TreeSet<ServiceDescription> inputPeersSet = new TreeSet<>();
//		inputPeersSet.add(new ServiceDescription("H4","http://localhost:8081/HotelWS/services/DataService4Port", dataLevel, lastModifiedDate));
//		inputPeersSet.add(new ServiceDescription("H10","http://localhost:8081/HotelWS/services/DataService10Port", dataLevel, lastModifiedDate));		
//		clientPeers.put("http://localhost:8081/HotelWS/services/DataService3Port",new PeersCache(inputPeersSet));
		
		clientPeers.put("http://localhost:8081/HotelWS/services/DataService3Port",new PeersCache(new ServiceDescription("H4","http://localhost:8081/HotelWS/services/DataService4Port", dataLevel, lastModifiedDate, C3)));
		clientPeers.put("http://localhost:8081/HotelWS/services/DataService4Port",new PeersCache(new ServiceDescription("H5","http://localhost:8081/HotelWS/services/DataService5Port", dataLevel, lastModifiedDate, C4)));
		clientPeers.put("http://localhost:8081/HotelWS/services/DataService5Port",new PeersCache(new ServiceDescription("H6","http://localhost:8081/HotelWS/services/DataService6Port", dataLevel, lastModifiedDate, C5)));
		clientPeers.put("http://localhost:8081/HotelWS/services/DataService6Port",new PeersCache(new ServiceDescription("H7","http://localhost:8081/HotelWS/services/DataService7Port", dataLevel, lastModifiedDate, C1)));
//		inputPeersSet = new TreeSet<>();
//		inputPeersSet.add(new ServiceDescription("H7","http://localhost:8081/HotelWS/services/DataService7Port", dataLevel, lastModifiedDate));
//		inputPeersSet.add(new ServiceDescription("H1","http://localhost:8081/HotelWS/services/DataService1Port", dataLevel, lastModifiedDate));
//		inputPeersSet.add(new ServiceDescription("H0","http://localhost:8081/HotelWS/services/DataServicePort", dataLevel, lastModifiedDate));
//		clientPeers.put("http://localhost:8081/HotelWS/services/DataService6Port",new PeersCache(inputPeersSet));
//		
		
		clientPeers.put("http://localhost:8081/HotelWS/services/DataService7Port",new PeersCache(new ServiceDescription("H8","http://localhost:8081/HotelWS/services/DataService8Port", dataLevel, lastModifiedDate, C2)));
		clientPeers.put("http://localhost:8081/HotelWS/services/DataService8Port",new PeersCache(new ServiceDescription("H9","http://localhost:8081/HotelWS/services/DataService9Port", dataLevel, lastModifiedDate, C3)));
		clientPeers.put("http://localhost:8081/HotelWS/services/DataService9Port",new PeersCache(new ServiceDescription("H10","http://localhost:8081/HotelWS/services/DataService10Port", dataLevel, lastModifiedDate, C4)));
		clientPeers.put("http://localhost:8081/HotelWS/services/DataService10Port",new PeersCache(new ServiceDescription("H11","http://localhost:8081/HotelWS/services/DataService11Port", dataLevel, lastModifiedDate, C1)));
		clientPeers.put("http://localhost:8081/HotelWS/services/DataService11Port",new PeersCache(new ServiceDescription("H0","http://localhost:8081/HotelWS/services/DataServicePort", dataLevel, lastModifiedDate, C5)));		
		
				
		

//		clientPeers.put("http://localhost:8081/HotelWS/services/DataService12Port",new PeersCache(new ServiceDescription("H13","http://localhost:8081/HotelWS/services/DataService13Port", dataLevel, lastModifiedDate)));
//		clientPeers.put("http://localhost:8081/HotelWS/services/DataService13Port",new PeersCache(new ServiceDescription("H14","http://localhost:8081/HotelWS/services/DataService14Port", dataLevel, lastModifiedDate)));
//		clientPeers.put("http://localhost:8081/HotelWS/services/DataService14Port",new PeersCache(new ServiceDescription("H15","http://localhost:8081/HotelWS/services/DataService15Port", dataLevel, lastModifiedDate)));
//		
//		clientPeers.put("http://localhost:8081/HotelWS/services/DataService15Port",new PeersCache(new ServiceDescription("H16","http://localhost:8081/HotelWS/services/DataService16Port", dataLevel, lastModifiedDate)));		
//		clientPeers.put("http://localhost:8081/HotelWS/services/DataService16Port",new PeersCache(new ServiceDescription("H17","http://localhost:8081/HotelWS/services/DataService17Port", dataLevel, lastModifiedDate)));
//		clientPeers.put("http://localhost:8081/HotelWS/services/DataService17Port",new PeersCache(new ServiceDescription("H18","http://localhost:8081/HotelWS/services/DataService18Port", dataLevel, lastModifiedDate)));
//		clientPeers.put("http://localhost:8081/HotelWS/services/DataService18Port",new PeersCache(new ServiceDescription("H19","http://localhost:8081/HotelWS/services/DataService19Port", dataLevel, lastModifiedDate)));
//		clientPeers.put("http://localhost:8081/HotelWS/services/DataService19Port",new PeersCache(new ServiceDescription("H20","http://localhost:8081/HotelWS/services/DataService20Port", dataLevel, lastModifiedDate)));
//		clientPeers.put("http://localhost:8081/HotelWS/services/DataService20Port",new PeersCache(new ServiceDescription("H21","http://localhost:8081/HotelWS/services/DataService21Port", dataLevel, lastModifiedDate)));
//		clientPeers.put("http://localhost:8081/HotelWS/services/DataService21Port",new PeersCache(new ServiceDescription("H22","http://localhost:8081/HotelWS/services/DataService22Port", dataLevel, lastModifiedDate)));
//		clientPeers.put("http://localhost:8081/HotelWS/services/DataService22Port",new PeersCache(new ServiceDescription("H23","http://localhost:8081/HotelWS/services/DataService23Port", dataLevel, lastModifiedDate)));
//		clientPeers.put("http://localhost:8081/HotelWS/services/DataService23Port",new PeersCache(new ServiceDescription("H24","http://localhost:8081/HotelWS/services/DataService24Port", dataLevel, lastModifiedDate)));
//		clientPeers.put("http://localhost:8081/HotelWS/services/DataService24Port",new PeersCache(new ServiceDescription("H25","http://localhost:8081/HotelWS/services/DataService25Port", dataLevel, lastModifiedDate)));
//		clientPeers.put("http://localhost:8081/HotelWS/services/DataService25Port",new PeersCache(new ServiceDescription("H26","http://localhost:8081/HotelWS/services/DataService26Port", dataLevel, lastModifiedDate)));
//		clientPeers.put("http://localhost:8081/HotelWS/services/DataService26Port",new PeersCache(new ServiceDescription("H27","http://localhost:8081/HotelWS/services/DataService27Port", dataLevel, lastModifiedDate)));
//		clientPeers.put("http://localhost:8081/HotelWS/services/DataService27Port",new PeersCache(new ServiceDescription("H28","http://localhost:8081/HotelWS/services/DataService28Port", dataLevel, lastModifiedDate)));
//		clientPeers.put("http://localhost:8081/HotelWS/services/DataService28Port",new PeersCache(new ServiceDescription("H29","http://localhost:8081/HotelWS/services/DataService29Port", dataLevel, lastModifiedDate)));
//		clientPeers.put("http://localhost:8081/HotelWS/services/DataService29Port",new PeersCache(new ServiceDescription("H30","http://localhost:8081/HotelWS/services/DataService30Port", dataLevel, lastModifiedDate)));
//		clientPeers.put("http://localhost:8081/HotelWS/services/DataService30Port",new PeersCache(new ServiceDescription("H0","http://localhost:8081/HotelWS/services/DataServicePort", dataLevel, lastModifiedDate)));


		clients.add(new WSClient());
		clients.add(new WSClient1());
		clients.add(new WSClient2());
		clients.add(new WSClient3());
		clients.add(new WSClient4());
		clients.add(new WSClient5());
		clients.add(new WSClient6());
		clients.add(new WSClient7());
		clients.add(new WSClient8());
		clients.add(new WSClient9());
		clients.add(new WSClient10());
		clients.add(new WSClient11());
		clients.add(new WSClient12());
		clients.add(new WSClient13());
		clients.add(new WSClient14());
		clients.add(new WSClient15());
		clients.add(new WSClient16());
		clients.add(new WSClient17());
		clients.add(new WSClient18());
		clients.add(new WSClient19());
		clients.add(new WSClient20());
		clients.add(new WSClient21());
		clients.add(new WSClient22());
		clients.add(new WSClient23());
		clients.add(new WSClient24());
		clients.add(new WSClient25());
		clients.add(new WSClient26());
		clients.add(new WSClient27());
		clients.add(new WSClient28());
		clients.add(new WSClient29());
		clients.add(new WSClient30());

		wsNames.put("http://localhost:8081/HotelWS/services/DataServicePort","H0");
		wsNames.put("http://localhost:8081/HotelWS/services/DataService1Port","H1");
		wsNames.put("http://localhost:8081/HotelWS/services/DataService2Port","H2");
		wsNames.put("http://localhost:8081/HotelWS/services/DataService3Port","H3");
		wsNames.put("http://localhost:8081/HotelWS/services/DataService4Port","H4");
		wsNames.put("http://localhost:8081/HotelWS/services/DataService5Port","H5");
		wsNames.put("http://localhost:8081/HotelWS/services/DataService6Port","H6");
		wsNames.put("http://localhost:8081/HotelWS/services/DataService7Port","H7");
		wsNames.put("http://localhost:8081/HotelWS/services/DataService8Port","H8");
		wsNames.put("http://localhost:8081/HotelWS/services/DataService9Port","H9");
		wsNames.put("http://localhost:8081/HotelWS/services/DataService10Port","H10");
		wsNames.put("http://localhost:8081/HotelWS/services/DataService11Port","H11");
		wsNames.put("http://localhost:8081/HotelWS/services/DataService12Port","H12");
		wsNames.put("http://localhost:8081/HotelWS/services/DataService13Port","H13");
		wsNames.put("http://localhost:8081/HotelWS/services/DataService14Port","H14");
		wsNames.put("http://localhost:8081/HotelWS/services/DataService15Port","H15");
		wsNames.put("http://localhost:8081/HotelWS/services/DataService16Port","H16");
		wsNames.put("http://localhost:8081/HotelWS/services/DataService17Port","H17");
		wsNames.put("http://localhost:8081/HotelWS/services/DataService18Port","H18");
		wsNames.put("http://localhost:8081/HotelWS/services/DataService19Port","H19");
		wsNames.put("http://localhost:8081/HotelWS/services/DataService20Port","H20");
		wsNames.put("http://localhost:8081/HotelWS/services/DataService21Port","H21");
		wsNames.put("http://localhost:8081/HotelWS/services/DataService22Port","H22");
		wsNames.put("http://localhost:8081/HotelWS/services/DataService23Port","H23");
		wsNames.put("http://localhost:8081/HotelWS/services/DataService24Port","H24");
		wsNames.put("http://localhost:8081/HotelWS/services/DataService25Port","H25");
		wsNames.put("http://localhost:8081/HotelWS/services/DataService26Port","H26");
		wsNames.put("http://localhost:8081/HotelWS/services/DataService27Port","H27");
		wsNames.put("http://localhost:8081/HotelWS/services/DataService28Port","H28");
		wsNames.put("http://localhost:8081/HotelWS/services/DataService29Port","H29");
		wsNames.put("http://localhost:8081/HotelWS/services/DataService30Port","H30");

	}

	public static PeersCache getPeersCache(String wsName) {
		if (clientPeers.containsKey(wsName))
			return clientPeers.get(wsName);
		throw new IllegalArgumentException();
	}

	public static IWSClient getWSClient(String name) {
		for (IWSClient client : clients)
			if (client.getWsAddress().equals(name))
				return client;
		throw new IllegalArgumentException();
	}

	public static String getWSName(String wsAddress) {
		return wsNames.get(wsAddress);
	}
}
